home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3780 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple i/o for my class
  5. Date: 31 Jan 1996 05:08:00 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4emtfg$2i2@news.iag.net>
  8. References: <4ekjql$5jl@nnrp1.news.primenet.com>
  9. NNTP-Posting-Host: pm1-orl14.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4ekjql$5jl@nnrp1.news.primenet.com>, mcoplea@primenet.com says...
  13. >
  14. >I am having a little trouble with a batting average program.
  15. >It seems to take the input OK, but always returns a '0' for the Batting 
  16. >Average.  Please look at the following code and make any suggestions.  Thank 
  17. >you in advance for any assistance you can offer. (email perfered)
  18. >
  19. >/***********************************
  20. >        DESIGN:
  21. >        Prompt the user to enter the number of at bats and the number of 
  22. hits.
  23. >        Calculate the batting average (number of hits / number of at bats)
  24. >        Print out the result with 3 significant places to the right of the
  25. >        decimal point and make sure that the average does not round up.
  26. >        Report the results back to the user.
  27. >
  28. >        TEST PLAN:
  29. >        Large Number / Large Number (11,000 / 2,125)
  30. >        Large Number / Small Number (12,000 / 33)
  31. >        Small Number / Small Number (15 / 6)
  32. >        Large Number / Zero (12,121 / 0)
  33. >        Small Number / Zero (5 / 0)
  34.  
  35. Divide by zero is illegal.  So, you will have to test for and handle it.
  36.  
  37. >
  38. >************************************/
  39. >
  40. >#include <stdio.h>
  41. >
  42. >int
  43. >main(void)
  44. >{
  45. >        float avar, hvar;      /*float variables(avar=At-bats,hvar=Hits */
  46. >        int Hitsint, Atbatsint;    /* Integers with Hits and At Bats */
  47. >        float Avg;                 /* Results Batting Average */
  48. >
  49. >        /* Prompt the user to input the data */
  50. >        printf("Enter the number of at-bats followed by hits:   ");
  51. >        scanf("%f %f", &avar, &hvar);
  52.  
  53. scanf is tricky to use for any input, especially multiple numbers on a 
  54. single line (users have a bad habit of not using exactly the format you 
  55. expect >-} This tends to really confuse scanf). You might want to use fgets 
  56. and sscanf (remember to test the return for the number of fields parsed).
  57.  
  58. >
  59. >        /* Convert to integers */
  60. >        Atbatsint = avar;
  61. >        Hitsint = hvar;
  62.  
  63. ?? I'm not sure why you scanf these into floats, when you apparently want 
  64. them in ints (and the type of values to be entered are inherently integral.
  65. At least I don't think you can have a fraction of a hit or at bat?).
  66.  
  67. >
  68. >        /* Calculate the Batting Average */
  69. >        Avg = Hitsint/Atbatsint;
  70.  
  71. This is where you are getting your 0.  Because Hitsint and AtBatsint are
  72. both ints, integer math is performed.  This means that any fractional
  73. component is truncated.  So, as long as AtBatsint is >= Hitsint, Avg will
  74. be assigned a value of 0.0.  
  75.  
  76. You can force floating point math by casting either or both of the operands
  77. to a floating point type (float, double, or long double).  Of course, you
  78. could simple use the floats you originally read the input into (ie use
  79. avar and hvar instead of Hitsint and AtBatsint).
  80.  
  81. >
  82. >        /* Return results to user */
  83. >        printf("Batting Average = %.2f\n", Avg);
  84.  
  85. Didn't the assignment specify precision to 3 places and protection against 
  86. rounding up?  This is 2 places and you will have rounding problems.
  87.  
  88. >
  89. >        return 0;                       /* We done */
  90. >}
  91. >
  92.  
  93. I strongly suggest that you d/l and read through the c.l.c faq (Frequently
  94. Asked Question) list.  It contains explanations and suggested solutions
  95. to many common problems in c programming.  It is available for anonymous ftp
  96. from rtfm.mit.edu /pub/usenet/comp.lang.c.
  97.  
  98. -- 
  99. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  100. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  101.  
  102.